package com.ihisun.tmhcms.core.service.withdrawalservice.impl;

import com.ihisun.tmhcms.core.dto.withdrawaldto.SearchWithdrawalDto;
import com.ihisun.tmhcms.core.service.withdrawalservice.WithdrawalService;
import com.ihisun.tmhcms.core.vo.UserWithdrawalNumberVo;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.List;
@Slf4j
@Service
public class WithdrawalServiceImpl implements WithdrawalService {
    @PersistenceContext
    EntityManager entityManager;

    @Override
    public ResponseVo searchWithdrawalRecord(SearchWithdrawalDto searchWithdrawalDto) {
        StringBuilder sql = new StringBuilder("select * from u_user_withdrawal_number uw INNER JOIN u_user uu on uw.uuid=uu.uuid");
        if (searchWithdrawalDto.getFrontMoney() != null && !"".equals(searchWithdrawalDto.getPostposition())) {
            sql.append(" and uw.withdrawal_amount >= " + searchWithdrawalDto.getFrontMoney() + " and uw.withdrawal_amount <= " + searchWithdrawalDto.getPostposition() + "");
        }
        if (searchWithdrawalDto.getFrontMoney() == "" && searchWithdrawalDto.getPostposition() != "") {
            sql.append(" and uw.withdrawal_amount <= " + searchWithdrawalDto.getPostposition() + "");
        }
        if (searchWithdrawalDto.getFrontMoney() != "" && searchWithdrawalDto.getPostposition() == "") {
            sql.append(" and uw.withdrawal_amount >= " + searchWithdrawalDto.getFrontMoney() + "");
        }
        if (searchWithdrawalDto.getFlowingWater() != "" && searchWithdrawalDto.getFlowingWater() != null) {
            sql.append(" and uw.flowing_water =" + searchWithdrawalDto.getFlowingWater() + "");
        }
        if (searchWithdrawalDto.getFrontDate() != "" && searchWithdrawalDto.getPostposDate() != "") {
            sql.append(" and uw.withdrawal_time between " + searchWithdrawalDto.getFrontDate() + " and " + searchWithdrawalDto.getPostposDate() + "");
        }
        if (searchWithdrawalDto.getFrontDate() != "" && searchWithdrawalDto.getPostposDate() == "") {
            sql.append(" and uw.withdrawal_time > " + searchWithdrawalDto.getFrontDate() + "");
        }
        if (searchWithdrawalDto.getFrontDate() == "" && searchWithdrawalDto.getPostposDate() != "") {
            sql.append(" and uw.withdrawal_time < " + searchWithdrawalDto.getPostposDate() + "");
        }
        if (searchWithdrawalDto.getName() != null && searchWithdrawalDto.getName() != "") {
            sql.append(" and uu.nick_name LIKE concat('%','" + searchWithdrawalDto.getName() + "','%')");
        }
        if (searchWithdrawalDto.getRole() != null && searchWithdrawalDto.getRole() != "") {
            sql.append(" AND uu.role =" + searchWithdrawalDto.getRole() + "");
        }
        if (searchWithdrawalDto.getPhone() != null && searchWithdrawalDto.getPhone() != "") {
            sql.append(" AND uu.phone like concat('%','" + searchWithdrawalDto.getPhone() + "','%')");
        }
        sql.append(" limit " + (searchWithdrawalDto.getPageNum() - 1) * 10 + "," + searchWithdrawalDto.getPageSize() + "");
        log.info("sql=:"+sql);
        List<UserWithdrawalNumberVo> resultList = entityManager.createNativeQuery(sql.toString(), UserWithdrawalNumberVo.class).getResultList();
        if (resultList != null && !resultList.isEmpty()) {
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i).getRole().equals("1")) {
                    resultList.get(i).setRole("普通会员");
                }
                if (resultList.get(i).getRole().equals("2")) {
                    resultList.get(i).setRole("pluss会员");
                }
                if (resultList.get(i).getRole().equals("3")) {
                    resultList.get(i).setRole("特约经销商");
                }
                if (resultList.get(i).getRole().equals("4")) {
                    resultList.get(i).setRole("高级经销商");
                }
                if (resultList.get(i).getRole().equals("5")) {
                    resultList.get(i).setRole("普通合伙人");
                }
                if (resultList.get(i).getRole().equals("6")) {
                    resultList.get(i).setRole("高级合伙人");
                }
                if (resultList.get(i).getState().equals("1")) {
                    resultList.get(i).setState("已到账");
                }
                if (resultList.get(i).getState().equals("2")) {
                    resultList.get(i).setState("未到账");
                }
                if (resultList.get(i).getState().equals("3")) {
                    resultList.get(i).setState("提现中");
                }
            }
        }

        StringBuilder countSql=new StringBuilder("select count(*) from u_user_withdrawal_number uw INNER JOIN u_user uu on uw.uuid=uu.uuid");

        if (searchWithdrawalDto.getFrontMoney() != null && !"".equals(searchWithdrawalDto.getPostposition())) {
            countSql.append(" and uw.withdrawal_amount >= " + searchWithdrawalDto.getFrontMoney() + " and uw.withdrawal_amount <= " + searchWithdrawalDto.getPostposition() + "");
        }
        if (searchWithdrawalDto.getFrontMoney() == "" && searchWithdrawalDto.getPostposition() != "") {
            countSql.append(" and uw.withdrawal_amount <= " + searchWithdrawalDto.getPostposition() + "");
        }
        if (searchWithdrawalDto.getFrontMoney() != "" && searchWithdrawalDto.getPostposition() == "") {
            countSql.append(" and uw.withdrawal_amount >= " + searchWithdrawalDto.getFrontMoney() + "");
        }
        if (searchWithdrawalDto.getFlowingWater() != "" && searchWithdrawalDto.getFlowingWater() != null) {
            countSql.append(" and uw.flowing_water =" + searchWithdrawalDto.getFlowingWater() + "");
        }
        if (searchWithdrawalDto.getFrontDate() != "" && searchWithdrawalDto.getPostposDate() != "") {
            countSql.append(" and uw.withdrawal_time between " + searchWithdrawalDto.getFrontDate() + " and " + searchWithdrawalDto.getPostposDate() + "");
        }
        if (searchWithdrawalDto.getFrontDate() != "" && searchWithdrawalDto.getPostposDate() == "") {
            countSql.append(" and uw.withdrawal_time > " + searchWithdrawalDto.getFrontDate() + "");
        }
        if (searchWithdrawalDto.getFrontDate() == "" && searchWithdrawalDto.getPostposDate() != "") {
            countSql.append(" and uw.withdrawal_time < " + searchWithdrawalDto.getPostposDate() + "");
        }
        if (searchWithdrawalDto.getName() != null && searchWithdrawalDto.getName() != "") {
            countSql.append(" and uu.nick_name LIKE concat('%','" + searchWithdrawalDto.getName() + "','%')");
        }
        if (searchWithdrawalDto.getRole() != null && searchWithdrawalDto.getRole() != "") {
            countSql.append(" AND uu.role =" + searchWithdrawalDto.getRole() + "");
        }
        if (searchWithdrawalDto.getPhone() != null && searchWithdrawalDto.getPhone() != "") {
            countSql.append(" AND uu.phone like concat('%','" + searchWithdrawalDto.getPhone() + "','%')");
        }
        log.info("countSql=:"+countSql);
        BigInteger singleResult =(BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();
        PageBean pageBean=new PageBean(searchWithdrawalDto.getPageNum(),singleResult.intValue(),searchWithdrawalDto.getPageSize(),resultList);

        return ResponseVo.ok(pageBean);
    }
}
